package test

import (
	"errors"
	"fmt"
	"github.com/jinzhu/gorm"
	user3 "server/app/api/request/user"
	"server/app/crons/server"
	"server/base"
	pool2 "server/common/models/pool"
	"server/common/models/user"
	user2 "server/common/service/user"
	"server/common/utils"
	"time"
)

var UserTest = userTest{}

type userTest struct {
	UserList []user.UserAuth
}

func (u *userTest) RegisterUser() {
	times := time.Now().Unix()
	for i := 0; i < 100; i++ {
		userOpenId := fmt.Sprintf("test_user_%d", int(times)+i)
		users, str, err := user2.AuthService.Login(userOpenId)
		if err != nil {
			fmt.Println("RegisterUser", str, err.Error())
			continue
		}

		// 修改名字和头像
		base.Mysql.Model(&user.User{}).Where("user_id = ?", users.Id).Update(map[string]interface{}{
			"nickname": fmt.Sprintf("test_user_%d", i+1),
			"avatar":   fmt.Sprintf("https://randomuser.me/api/portraits/%s/%d.jpg", utils.If(i%2 == 0, "men", "women"), i),
			"gender":   utils.If(i%2 == 0, 1, 2),
			"type":     "none",
		})

		u.UserList = append(u.UserList, users)
	}
	fmt.Println("RegisterUser", "注册用户成功")
}

func (u *userTest) RegisterPool() {
	for _, item := range u.UserList {
		var PoolRegister pool2.PoolUserRegister
		err := base.App.Mysql.Object.Transaction(func(tx *gorm.DB) error {
			tx.Find(&PoolRegister, "user_id = ?", item.Id)
			if PoolRegister.UserId != 0 {
				return errors.New("重复操作")
			}
			users := user.User{}
			base.Mysql.Find(&users, "user_id = ?", item.Id)
			PoolRegister.UserId = item.Id
			PoolRegister.Status = "wait"
			PoolRegister.UserGender = users.Gender
			PoolRegister.CreateTime = time.Now().Unix()
			PoolRegister.UpdateTime = time.Now().Unix()
			tx.Create(&PoolRegister)
			return nil
		})
		if err != nil {
			fmt.Println("RegisterPool", err.Error())
			return
		}
	}
	fmt.Println("RegisterPool", "报名成功")
}

func (u *userTest) MatchPool() {
	pool := server.PostPool{}
	pool.Run()
}

func (u *userTest) Moment() {
	fmt.Println("Moment", "开始动态")
	for i, item := range u.UserList {
		req := user3.MomentCreate{Content: fmt.Sprintf("这是第%d条动态", i+1), Status: "all", Type: "show"}
		userMoment := req.ToModel(item.Id)
		base.App.Mysql.Object.Create(&userMoment)
	}
	fmt.Println("Moment", "动态完成")
}

func (u *userTest) MatchFired() {
	fmt.Println("Addfirend", "开始生成")
	//for i, item := range u.UserList {
	//	for j, item2 := range u.UserList {
	//		if i == j {
	//			continue
	//		}
	//		req := user3.AddFriend{
	//			UserId:   item.Id,
	//			FriendId: item2.Id,
	//		}
	//		userMoment := req.ToModel()
	//		base.App.Mysql.Object.Create(&userMoment)
	//	}
	//}
}
